<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>
<script>
import * as echarts from "echarts";
import resize from "../../index/comment/mixins/resize";
export default {
  mixins: [resize],
  data() {
    return {
      chart: null,
    };
  },
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "600px",
    },
    Chartdata: {
      type: Object,
      required: true,
    },
    theme: {
      type: String,
      default: "",
    },
    titlevalue:{
      // type: Array,
      default: "",
    }
  },

  watch:{
    watchtitle:{
        handler(){
            this.initChart();
        },
        deep:true
    }
  },
  computed:{
       watchtitle(){
          let type=this.titlevalue;
          return type
       }
  },
  mounted() {
    this.$nextTick(() => {
       this.chart = echarts.init(this.$el);
      this.initChart();
    });
  },
  methods: {
    initChart() {
      let series = [];
      let legend = [];
      let xAxis = [];
      let color = [];
      let typle = {};
      let formatter='{value}'
      this.Chartdata.data.forEach((item) => {
        series.push({
          name: item.title,
          type:this.Chartdata.type,
          stack: "Total",
          areaStyle: {},
          emphasis: {
            focus: "series",
          },
          data: item.list,
        });
        legend.push(item.title);
        xAxis.push(item.time);
        color.push(item.color);
        if(this.titlevalue.includes(item.title)){
              typle[item.title]=true;
        }else{
              typle[item.title]=false;
        }
      });
      if(this.titlevalue.includes('点击率','填充率')){
          formatter='{value} %';
      }
      this.setOptions(legend, xAxis, series, this.Chartdata.title,color ,typle ,formatter);
    },
    setOptions(legend, xAxis, series,title,color,typle,formatter) {
      this.chart.setOption({
        color: color,
        title: {
          text: title,
          left: "left",
          show:false,
          textStyle: {
            color: "#888",
            fontWeight: "normal",
            fontSize: "12px",
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#6a7985",
            },
          },
        },
        legend: {
          data: legend,
          show:false,
          selected:typle
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        grid: {
          left: "2%",
          right: "2%",
          bottom: "2%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: this.Chartdata.type=='line'?false:true,
            data: xAxis,
          },
        ],
        yAxis: [
          {
            type: "value",
            axisLabel: {
                formatter: formatter //设置单位为百分比
            }
          },
        ],
        series: series,
      });
    },
  },
};
</script>
